// pages/activity/evaluationDescription/index.js
const api = require("../../../config/api")
const request = require("../../../utils/request")
const app = getApp()
const { getStorage } = require("../../../utils/util")
let isNavigate = 0
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		btn1:' https://kj-zhiliao.oss-cn-qingdao.aliyuncs.com/zhiliao-applet/SbheGBH6iSRR94179d0a903171c0e96b2b09402cb500.png',
		btn2:' https://kj-zhiliao.oss-cn-qingdao.aliyuncs.com/zhiliao-applet/GYaofNQNS0Hh289e6f04997cc8ae0f91a66215f9414e.png',

		showTip:false,
		showTopInfo:false,
		hadLogin: false,
    patientInfo:{},
    isChild: 1, // 是孩子还是家长 1-孩子 2-家长
    isCurrentStorage: 0,// 是否是当前缓存
    btnBottom: app.kBottomSafeHeight + 130,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let {hadLogin} = this.data
		console.log('app.isLogin()',app.isLogin());
		console.log('hadLogin',hadLogin);
    let patientInfo = wx.getStorageSync('patientInfo')
    
		if(app.isLogin() && patientInfo?.id){
			this.setData({
				showTopInfo:true,
				patientInfo,
			})
      //判断是否是当前缓存--该页面只有一种情况，孩子缓存
      let patientId = patientInfo.id
      let userId = wx.getStorageSync('userId')
      let evaluationUserId = getStorage("evaluationUserId")
      let evaluationPatientId = getStorage("evaluationPatientId")
      let evaluationAnswerList = getStorage("evaluationAnswerList")
      let evaluationIsChild = getStorage("evaluationIsChild")

      if( evaluationAnswerList.length>0 && userId === evaluationUserId && patientId === evaluationPatientId && evaluationIsChild === 1){
        this.setData({
          isCurrentStorage: 1
        })
      }
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
    this.setData({
      hadLogin: app.isLogin()
    })
	},
	// 点击开始评估
	handleBegin(){
		let { hadLogin, isCurrentStorage } = this.data
    const patientInfo = wx.getStorageSync('patientInfo')
		if(hadLogin){
			if(patientInfo?.id){
        if(isCurrentStorage === 1){
          this.setData({
            showTip: true
          })
        }else{
          isNavigate = 1
          wx.redirectTo({
            url: '/pages/activity/evaluationAnswer/index?isChild=1',
          })
        }
			}else{
        app.globalData.createPatientEntrance = 10
				wx.navigateTo({
					url: '/pages/createPatient/select/select',
				})
			}
		}else{
			app.navTo('/pages/activity/evaluationAnswer/index')
		}
  },
  // 重新评估
  handleReturn(){
    let { isCurrentStorage } = this.data
    if(isCurrentStorage === 1){
      app.removeQuestionStorage()
    }
    this.setData({
      showTip: false
    })
    isNavigate = 1
    wx.redirectTo({
      url: '/pages/activity/evaluationAnswer/index?isChild=1',
    })
  },
  // 继续评估
  handleContinue(){
    this.setData({
      showTip: false
    })
    isNavigate = 1
    wx.redirectTo({
      url: '/pages/activity/evaluationAnswer/index?isChild=1',
    })
  },
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
    if(isNavigate === 0){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
})
